<?php

class ChiController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }

        if ($action == 'remove') {
            // mảng danh sách giá trị id
            $values = $_POST["id"];

            $result = $this->deleteChi($values);


            if ($result == OK) {
//                Utility::messageBox('Xóa thành công');
                echo'OK';
            }
            exit();
        } else if ($action == 'add') {
            $BEP_ID = $_POST['BEP_ID'];
            $NHAP_NGAY_NHAP = $_POST['NHAP_NGAY_NHAP'];
            $LY_DO_NHAP = $_POST['LY_DO_NHAP'];
            $NHAP_TONG_TIEN_PHAI_TRA = $_POST['NHAP_TONG_TIEN_PHAI_TRA'];
            $DON_GIA = $_POST['DON_GIA'];
            $SOLUONG = $_POST['SOLUONG'];
            $THANHTIEN = $_POST['THANHTIEN'];
            $ID_HH = $_POST['ID_HH'];
            $result = $this->addNhap($BEP_ID, $NHAP_NGAY_NHAP, $LY_DO_NHAP, $NHAP_TONG_TIEN_PHAI_TRA, $DON_GIA, $SOLUONG, $THANHTIEN, $ID_HH);
            if ($result == OK) {
                if ($action == 'add') {
                    echo 'OK';
                }
            }
            exit();
        } else if ($action == 'edit') {

            $NHAP_ID = $_POST['NHAP_ID'];
            $BEP_ID = $_POST['BEP_ID'];
            $NHAP_NGAY_NHAP = $_POST['NHAP_NGAY_NHAP'];
            $LY_DO_NHAP = $_POST['LY_DO_NHAP'];
            $NHAP_TONG_TIEN_PHAI_TRA = $_POST['NHAP_TONG_TIEN_PHAI_TRA'];
            $DON_GIA = $_POST['DON_GIA'];
            $SOLUONG = $_POST['SOLUONG'];
            $THANHTIEN = $_POST['THANHTIEN'];
            $ID_HH = $_POST['ID_HH'];
            $result1 = $this->deleteChiTietNhap($NHAP_ID);
            $result2 = $this->deleteChi_chitiet($NHAP_ID);
            $result = $this->updateNhap($NHAP_ID, $BEP_ID, $NHAP_NGAY_NHAP, $LY_DO_NHAP, $NHAP_TONG_TIEN_PHAI_TRA, $DON_GIA, $SOLUONG, $THANHTIEN, $ID_HH);
            if ($result == OK) {
                if ($action == 'edit') {
                    echo 'OK';
                }
            }
            exit();
        }


        if ($task == 'add') {
            $xtemplate = new XTemplate('./components' . DS . 'com_logic_chi' . DS . 'views' . DS . 'chi_add.php');

            $beps = $this->getAllBep();

            if ($beps != null) {

                foreach ($beps as $bep) {


                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep');
                }
            }
            $hanghoas = $this->getAllHangHoa();


            if ($hanghoas != null) {

                foreach ($hanghoas as $hanghoa) {

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP', $hanghoa->getDON_GIA_NHAP());
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());




                    $xtemplate->parse('main.hanghoa');
                }
            }

            $hanghoas_gia_soluongs = $this->getAllHangHoa();


            if ($hanghoas_gia_soluongs != null) {

                foreach ($hanghoas_gia_soluongs as $hanghoa) {

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP', $hanghoa->getDON_GIA_NHAP());
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());




                    $xtemplate->parse('main.hanghoas_gia_soluong');
                }
            }


            $xtemplate->assign('DATE', Utility::getCurrentDate());
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        // nếu task là edit thì include trang thêm vào
        else if ($task == 'edit') {
            $id = $_GET['idnhap'];
            $xtemplate = new XTemplate('./components' . DS . 'com_logic_chi' . DS . 'views' . DS . 'chi_edit.php');
            $chi = $this->getNhapById($id);
//            Utility::messageBox($id);

            if ($chi != null) {


                $xtemplate->assign('NHAP_ID', $chi->getNHAP_ID());
                $xtemplate->assign('BEP_ID', $chi->getBEP_ID());
                 $xtemplate->assign('NHAP_SO_PHIEU_NHAP', $chi->getNHAP_SO_PHIEU_NHAP());

                $xtemplate->assign('NHAP_NGAY_NHAP', $chi->getNHAP_NGAY_NHAP());
                $xtemplate->assign('NHAP_LY_DO_NHAP', $chi->getNHAP_LY_DO_NHAP());
                $xtemplate->assign('NHAP_TONG_TIEN_PHAI_TRA', Utility::formatToCurrencyString($chi->getNHAP_TONG_TIEN_PHAI_TRA()));
                $beps = $this->getAllBep();
                $option_bep = '';
                if ($beps != null) {

                    foreach ($beps as $bep) {


                        if ($chi->getBEP_ID() == $bep->getBEP_ID()) {
                            $xtemplate->assign('BEP', $bep->getBEP_TEN_BEP());
                        }
                    }
                }


                $chitietnhaps = $this->getChiTietNhapById($id);
                if ($chitietnhaps != null) {

                    foreach ($chitietnhaps as $chitietnhap) {

                        $xtemplate->assign('H_ID', $chitietnhap->getHH_ID());
                        $xtemplate->assign('H_TEN', $this->getTenHH($chitietnhap->getHH_ID())->getTEN_HANG_HOA());
                        $xtemplate->assign('H_GIA_NHAP', Utility::formatToCurrencyString($chitietnhap->getCT_DON_GIA()));
                        $xtemplate->assign('SO_LUONG', $chitietnhap->getCT_SO_LUONG());
                        $xtemplate->parse('main.nhap.hanghoa');
                    }
                }




                $xtemplate->parse('main.nhap');
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
        } else {
            $viewFile = $dir . '/views/chi.php';
            $xtemplate = new XTemplate($viewFile);
            
            $chis = $this->getChiByPhanLoai("nhaphang");
           

            if ($chis != null) {
                foreach ($chis as $chi) {

                     $chinhaphangs=$this->getChiNhapHang($chi->getCHI_ID());
                    
                     $nhaps=$this->getNhapHang($chinhaphangs->getNHAP_ID());
                    
                    $xtemplate->assign('NHAP_ID', $nhaps->getNHAP_ID());
                    $xtemplate->assign('NHAP_NGAY_NHAP', Utility::formatDateTimeToView($nhaps->getNHAP_NGAY_NHAP()));
                    $xtemplate->assign('NHAP_SO_PHIEU_NHAP', $nhaps->getNHAP_SO_PHIEU_NHAP());

                    $xtemplate->assign('BEP', $this->getBepById($nhaps->getBEP_ID())->getBEP_TEN_BEP());
                    $xtemplate->assign('NHAP_TONG_TIEN_PHAI_TRA', Utility::formatToCurrencyString($nhaps->getNHAP_TONG_TIEN_PHAI_TRA()));
                    $xtemplate->assign('NHAP_TONG_TIEN_DA_TRA', Utility::formatToCurrencyString($nhaps->getNHAP_TONG_TIEN_DA_TRA()));




                    $xtemplate->parse('main.chinhaphang');
                }
            }



            
            
            
            
            
            //CHI CHO HOA HỒNG
            $chis_hh = $this->getChiByPhanLoai("hoahong");
            $db=new Database();
            
            if ($chis_hh != null) {
                $INDEX=1;
                $TRUONG_=''; 
                foreach ($chis_hh as $chi_hh) {

                     $chihoahong_d=new Chi_hoa_hong();
                     $chihoahong_d->setCHI_ID($chi_hh->getCHI_ID());
                     $chihoahong_ds=$chihoahong_d->getByID_CHI($db);
                     
                     $trahoahong=new Tra_hoa_hong();
                     $trahoahong->setTHH_ID($chihoahong_ds->getTHH_ID());
                     $trahoahongs=$trahoahong->getByID($db);
                     
                     $loaihoahong=new LoaiHoaHong();
                     $loaihoahong->setLHH_ID($trahoahongs->getLHH_ID());
                     $loaihoahongs=$loaihoahong->getByID($db);
                     
                     
                     $xuat=new Xuat();
                     $xuat->setXUAT_ID($trahoahongs->getXUAT_ID());
                     $xuats=$xuat->getByID($db);
                     
                     $truong=new Truong();
                     $truong->setTRUONG_ID($xuats->getTRUONG_ID());
                     $truongs=$truong->getByID($db);
                     
                     $bep=new Bep();
                     $bep->setBEP_ID($truongs->getBEP_ID());
                     $beps=$bep->getByID($db);
                    
                     
                    $xtemplate->assign('CHI_ID', $chi_hh->getCHI_ID()); 
                    if(($INDEX == 1 ) || ($truongs->getTRUONG_ID()!= $TRUONG_ )){
                         $xtemplate->assign('TRUONG_TEN_TRUONG',' <li data-role="list-divider">Trường: '.$truongs->getTRUONG_TEN_TRUONG().' - Bếp: '.$beps->getBEP_TEN_BEP().'</li>' );
                }else{
                         $xtemplate->assign('TRUONG_TEN_TRUONG','' );
                
                }
                   $TRUONG_=$truongs->getTRUONG_ID();
                   
                    $xtemplate->assign('SO_TIEN', Utility::formatToCurrencyString($trahoahongs->getSO_TIEN()));
                    $xtemplate->assign('LHH_TEN_LOAI', $loaihoahongs->getLHH_TEN_LOAI());

                    $xtemplate->assign('BEP', $beps->getBEP_TEN_BEP());
                    $xtemplate->assign('NHAP_NGAY_NHAP', Utility::formatDateTimeToView($chi_hh->getCHI_NGAY_CHI()));
                   
                    

                    $INDEX++;
                    $xtemplate->parse('main.chihoahong');
                    
                }
            }
            
            
            
            //CHI CHO HOA HỒNG
            $chis_thue = $this->getChiByPhanLoai("thue");
           

            if ($chis_thue != null) {
                foreach ($chis_thue as $chi_thue) {

                      $chithue=new Chi_thue();
                      $chithue->setCHI_ID($chi_thue->getCHI_ID());
                      $chithues=$chithue->getByID_CHI($db);
                     
                      $trathue=new Tra_thue();
                      $trathue->setTT_ID($chithues->getTT_ID());
                      $trathues=$trathue->getByID($db);
                     
                    
                     
                     $xuat=new Xuat();
                     $xuat->setXUAT_ID($trathues->getXUAT_ID());
                     $xuats=$xuat->getByID($db);
                     
                     $truong=new Truong();
                     $truong->setTRUONG_ID($xuats->getTRUONG_ID());
                     $truongs=$truong->getByID($db);
                     
                     $bep=new Bep();
                     $bep->setBEP_ID($truongs->getBEP_ID());
                     $beps=$bep->getByID($db);
                    
                    
                    $xtemplate->assign('CHI_ID', $chi_thue->getCHI_ID()); 
                    $xtemplate->assign('TRUONG_TEN_TRUONG', $truongs->getTRUONG_TEN_TRUONG());
                    $xtemplate->assign('SO_TIEN', Utility::formatToCurrencyString($trathues->getSO_TIEN()));

                    $xtemplate->assign('BEP', $beps->getBEP_TEN_BEP());
                    $xtemplate->assign('NHAP_NGAY_NHAP', Utility::formatDateTimeToView($chi_thue->getCHI_NGAY_CHI()));
                   



                    $xtemplate->parse('main.chithue');
                }
            }
            
            

            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
    }

    private function getHoaHongById($id, $truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getByID($id, $truong, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getTenHH($ID) {
        $db = new Database();
        try {
            $hanghoa = new Hanghoa();
            $hanghoa->setID($ID);
            $result = $hanghoa->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getArrayHoaHongById($truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getArrayByID($truong, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getAllLoaiHoaHong() {
        $db = new Database();
        try {
            $loaihoahong = new LoaiHoaHong();

            $result = $loaihoahong->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function updateNhap($NHAP_ID, $BEP_ID, $NHAP_NGAY_NHAP, $LY_DO_NHAP, $NHAP_TONG_TIEN_PHAI_TRA, $DON_GIA, $SOLUONG, $THANHTIEN, $ID_HH) {

        $db = new Database();
        try {


            $nhap = new Nhap();
            $nhap->setNHAP_ID($NHAP_ID);
            $nhap->setBEP_ID($BEP_ID);
            $date = new DateTime($NHAP_NGAY_NHAP);
            $nhap->setNHAP_NGAY_NHAP($date->format("Y-m-d"));
            $nhap->setNHAP_LY_DO_NHAP($LY_DO_NHAP);
            $nhap->setNHAP_TONG_TIEN_PHAI_TRA($NHAP_TONG_TIEN_PHAI_TRA);
            $nhap->setNHAP_TONG_TIEN_DA_TRA($NHAP_TONG_TIEN_PHAI_TRA);
            $nhap->setIS_ACTIVE(1);
            $nhap->update($db);

            $chi = new Chi();
            $chi->setCHI_NGAY_CHI($date->format("Y-m-d"));
            $chi->setNHAP_ID($nhap->getNHAP_ID());
            $chi->setCHI_SO_TIEN($NHAP_TONG_TIEN_PHAI_TRA);
            $chi->setIS_ACTIVE(1);
            $chi->insert($db);


            $DON_GIA_ = explode(',', $DON_GIA);
            $SOLUONG_ = explode(',', $SOLUONG);
            $THANHTIEN_ = explode(',', $THANHTIEN);
            $ID_HH_ = explode(',', $ID_HH);
            for ($i = 0; $i < sizeof($ID_HH_); $i++) {

                $chitietnhap = new Chi_tiet_nhap();
                $chitietnhap->setNHAP_ID($nhap->getNHAP_ID());
                $chitietnhap->setHH_ID($ID_HH_[$i]);
                $chitietnhap->setCT_DON_GIA($DON_GIA_[$i]);
                $chitietnhap->setCT_SO_LUONG($SOLUONG_[$i]);
                $chitietnhap->setCT_THANH_TIEN($THANHTIEN_[$i]);
                $chitietnhap->setIS_ACTIVE(1);
                $chitietnhap->insert($db);
            }



            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    //----------------------đang sử dụng 


    private function deleteChiTietNhap($values) {
        $db = new Database();
        try {

            $chitietnhap = new Chi_tiet_nhap();
            $chitietnhap->setNHAP_ID($values);
            $chitietnhap->delete($db);

            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function deleteChi_chitiet($values) {
        $db = new Database();
        try {

            $chi = new Chi();
            $chi->setNHAP_ID($values);
            $chi->delete($db);

            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getChiTietNhapById($chi) {
        $db = new Database();
        try {
            $chitietnhap = new Chi_tiet_nhap();
            $result = $chitietnhap->getByID_ChiTiet($chi,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getChiByPhanLoai($phanloai) {
        $db = new Database();
        try {
            $chi = new Chi();

            $result = $chi->getChiByPhanLoai($phanloai,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getNhapById($id) {
        $db = new Database();
        try {
            $nhap = new Nhap();
            $nhap->setNHAP_ID($id);
            $result = $nhap->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
 private function getChiNhapHang($id) {
        $db = new Database();
        try {
            $chi_nhap_hang = new Chi_nhap_hang();
            $chi_nhap_hang->setCHI_ID($id);
            $result = $chi_nhap_hang->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
     private function getNhapHang($id) {
        $db = new Database();
        try {
            $nhap = new Nhap();
            $nhap->setNHAP_ID($id);
            $result = $nhap->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getBepById($id) {
        $db = new Database();
        try {
            $bep = new Bep();
            $bep->setBEP_ID($id);
            $result = $bep->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function addNhap($BEP_ID, $NHAP_NGAY_NHAP, $LY_DO_NHAP, $NHAP_TONG_TIEN_PHAI_TRA, $DON_GIA, $SOLUONG, $THANHTIEN, $ID_HH) {

        $db = new Database();
        try {


            $nhap = new Nhap();
            $nhap->setBEP_ID($BEP_ID);

            $date = new DateTime($NHAP_NGAY_NHAP);
            $nhap->setNHAP_NGAY_NHAP($date->format("Y-m-d"));
            $nhap->setNHAP_LY_DO_NHAP($LY_DO_NHAP);
            $nhap->setNHAP_TONG_TIEN_PHAI_TRA($NHAP_TONG_TIEN_PHAI_TRA);
            $nhap->setNHAP_TONG_TIEN_DA_TRA($NHAP_TONG_TIEN_PHAI_TRA);
            $nhap->setIS_ACTIVE(1);
            $nhap->insert($db);
//                echo 'aa';
//            exit();
            $chi = new Chi();
            $chi->setCHI_NGAY_CHI($date->format("Y-m-d"));
            $chi->setNHAP_ID($nhap->getNHAP_ID());
            $chi->setCHI_SO_TIEN($NHAP_TONG_TIEN_PHAI_TRA);
            $chi->setIS_ACTIVE(1);
            $chi->insert($db);

            $DON_GIA_ = explode(',', $DON_GIA);
            $SOLUONG_ = explode(',', $SOLUONG);
            $THANHTIEN_ = explode(',', $THANHTIEN);
            $ID_HH_ = explode(',', $ID_HH);
            for ($i = 0; $i < sizeof($ID_HH_); $i++) {

                $chitietnhap = new Chi_tiet_nhap();
                $chitietnhap->setNHAP_ID($nhap->getNHAP_ID());
                $chitietnhap->setHH_ID($ID_HH_[$i]);
                $chitietnhap->setCT_DON_GIA($DON_GIA_[$i]);
                $chitietnhap->setCT_SO_LUONG($SOLUONG_[$i]);
                $chitietnhap->setCT_THANH_TIEN($THANHTIEN_[$i]);
                $chitietnhap->setIS_ACTIVE(1);
                $chitietnhap->insert($db);
            }



            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getAllBep() {
        $db = new Database();
        try {
            $bep = new Bep();

            $result = $bep->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getAllHangHoa() {
        $db = new Database();
        try {
            $hanghoa = new Hanghoa();

            $result = $hanghoa->getList_($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function deleteChi($values) {
        $db = new Database();
        try {

            $nhap = new Nhap();
            $nhap->setNHAP_ID($values);
            $nhap->delete($db);

            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}
